package cn.yuanqiao.common.config;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import java.util.Map;

/**
 * 大写转换
 *
 * @ProjectName: cn.yuanqiao.archive.config
 * @ClassName: MapKeyUpperCaseWrapperFactory
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2024/4/3 16:51
 * @Version: 1.0
 **/
public class MapKeyUpperCaseWrapperFactory implements ObjectWrapperFactory {
    public MapKeyUpperCaseWrapperFactory() {
    }

    @Override
    public boolean hasWrapperFor(Object object) {
        if (object instanceof Map) {
            return true;
        }
        return false;
    }

    @Override
    public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
        if (object instanceof Map) {
            return new CustomMapWrapper(metaObject, (Map) object);
        }
        return null;
    }

    public static class CustomMapWrapper extends MapWrapper {

        public CustomMapWrapper(MetaObject metaObject, Map<String, Object> map) {
            super(metaObject, map);
        }

        @Override
        public String findProperty(String name, boolean useCamelCaseMapping) {
            if (name != null && name.length() > 0) {
                return name.toUpperCase();
            }
            return super.findProperty(name, useCamelCaseMapping);
        }
    }

}

//
//package com.bytedance.douyin.config;
//        import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
//        import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
//        import org.springframework.core.convert.converter.Converter;
//        import org.springframework.stereotype.Component;
//
//@Component
//@ConfigurationPropertiesBindingpublic
//class ObjectWrapperFactoryConverter implements Converter {
//    @Override
//    public ObjectWrapperFactory convert(String source) {
//        try {
//            return (ObjectWrapperFactory) Class.forName(source).newInstance();
//        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}
//
//
//    @Beanpublic
//    ConfigurationCustomizer mybatisConfigurationCustomizer() {
//        return new ConfigurationCustomizer() {
//            @Override
//            public void customize(org.apache.ibatis.session.Configuration configuration) {
//                configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
//            }
//        };
//    }